package com.sky.business.sys.event;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.sky.business.sys.domain.CameraConfig;
import com.sky.common.enums.ControlMode;
import com.sky.common.utils.NumberUtil;
import lombok.Data;

import java.math.BigDecimal;

/**
 * 设备信息,用于CameraStatus、CreateDevice事件
 */
@Data
public class DeviceInfo {
    /**
     * 设备名称
     */
    private String deviceName;
    /**
     * 设备类型
     */
    private String deviceType = "oe";


    /**
     * ip地址
     */
    private String deviceIP;
    /**
     * onvif端口号
     */
    private Integer onvifHttpPort;
    /**
     * rtsp端口号
     */
    private Integer onvifRtspPort;

    /**
     * onvif用户名
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String onvifUsername;

    /**
     * onvif密码
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String onvifPassword;

    /**
     * 制造商
     */
    private String manufacturer;

    /**
     * 型号
     */
    private String model;

    /**
     * 固件版本
     */
    private String firmwareVersion;

    /**
     * 序列号
     */
    private String serialNumber;


    /**
     * 纬度
     */
    private BigDecimal latitude;
    /**
     * 经度
     */
    private BigDecimal longitude;
    /**
     * 海拔高度
     */
    private BigDecimal altitude;

    /**
     * 水平偏差角
     */
    private BigDecimal panAngleTrim;

    /**
     * 垂直偏差角
     */
    private BigDecimal tiltAngleTrim;

    /**
     * 水平最小角度
     */
    private Integer panAngleMin = -180;

    /**
     * 水平最大角度
     */
    private Integer panAngleMax = 180;

    /**
     * 垂直最小角度
     */
    private BigDecimal tiltAngleMin;

    /**
     * 垂直最大角度
     */
    private BigDecimal tiltAngleMax;

    /**
     * 水平市场角
     */
    private BigDecimal horizontalFov;

    /**
     * 垂直视场角
     */
    private BigDecimal verticalFov;

    /**
     * 最小焦距
     */
    private BigDecimal focalLenMin;

    /**
     * 最大焦距
     */
    private BigDecimal focalLenMax;

    /**
     * 光学变倍最大值
     */
    private Integer zoomIndexMax;

    /**
     * 最小检测距离
     */
    private Integer detectMinDist;

    /**
     * 最大检测距离
     */
    private Integer detectMaxDist;

    /**
     * 推流地址
     */
    private String originalPushStream;

    /**
     * ai
     */
    private String aiPushStream;

    /**
     * 是否倒转 1:正装  -1:倒装
     */
    private Integer reverse;

    /**
     * 是否智能硬件
     */
    private Integer smart;

    /**
     * 转换为相机设备
     * @return 相机设备
     */
    public CameraConfig toCameraConfig(){
        CameraConfig cameraConfig = new CameraConfig();
        cameraConfig.setCameraDeviceName(deviceName);
        cameraConfig.setCameraIp(deviceIP);
        cameraConfig.setCameraPort(String.valueOf(onvifHttpPort));
        cameraConfig.setCameraRtspPort(String.valueOf(onvifRtspPort));
        cameraConfig.setOnvifUser(onvifUsername);
        cameraConfig.setOnvifPasswd(onvifPassword);
        cameraConfig.setCameraLatitude(String.valueOf(latitude));
        cameraConfig.setCameraLongitude(String.valueOf(longitude));
        cameraConfig.setCameraAltitude(String.valueOf(altitude));
        cameraConfig.setPanAngle(String.valueOf(panAngleTrim));
        cameraConfig.setTiltAngle(String.valueOf(tiltAngleTrim));
        cameraConfig.setCameraTiltRangeMin(String.valueOf(tiltAngleMin));
        cameraConfig.setCameraTiltRangeMax(String.valueOf(tiltAngleMax));
        cameraConfig.setHorizontalFov(horizontalFov);
        cameraConfig.setVerticalFov(verticalFov);
        cameraConfig.setCameraFocalLenMin(String.valueOf(focalLenMin));
        cameraConfig.setCameraFocalLenMax(String.valueOf(focalLenMax));
        cameraConfig.setCameraZoomRangeMax(String.valueOf(zoomIndexMax));
        cameraConfig.setDetectMinDist(detectMinDist);
        cameraConfig.setDetectMaxDist(detectMaxDist);
        cameraConfig.setPushStream(originalPushStream);
        cameraConfig.setAiPushStream(aiPushStream);
        cameraConfig.setReverse(reverse==-1);
        cameraConfig.setSmart(smart==1);
        cameraConfig.setControlMode(ControlMode.onvif);
        return cameraConfig;
    }

    /**
     * 把设备记录转换为设备信
     * @param cameraConfig 设备记录
     * @return 设备信息
     */
    public static DeviceInfo valueOf(CameraConfig cameraConfig){
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setDeviceName(cameraConfig.getCameraDeviceName());
        deviceInfo.setDeviceIP(cameraConfig.getCameraIp());
        deviceInfo.setOnvifHttpPort(NumberUtil.parseInt(cameraConfig.getCameraPort().trim()));
        deviceInfo.setOnvifRtspPort(NumberUtil.parseInt(cameraConfig.getCameraRtspPort()));
        deviceInfo.setLatitude(NumberUtil.toDecimal(cameraConfig.getCameraLatitude()));
        deviceInfo.setLongitude(NumberUtil.toDecimal(cameraConfig.getCameraLongitude()));
        deviceInfo.setAltitude(NumberUtil.toDecimal(cameraConfig.getCameraAltitude()));
        deviceInfo.setPanAngleTrim(NumberUtil.toDecimal(cameraConfig.getPanAngle()));
        deviceInfo.setTiltAngleTrim(NumberUtil.toDecimal(cameraConfig.getTiltAngle()));
        deviceInfo.setTiltAngleMin(NumberUtil.toDecimal(cameraConfig.getCameraTiltRangeMin()));
        deviceInfo.setTiltAngleMax(NumberUtil.toDecimal(cameraConfig.getCameraTiltRangeMax()));
        deviceInfo.setHorizontalFov(cameraConfig.getHorizontalFov());
        deviceInfo.setVerticalFov(cameraConfig.getVerticalFov());
        deviceInfo.setFocalLenMin(NumberUtil.toDecimal(cameraConfig.getCameraFocalLenMin()));
        deviceInfo.setFocalLenMax(NumberUtil.toDecimal(cameraConfig.getCameraFocalLenMax()));
        deviceInfo.setZoomIndexMax(NumberUtil.parseInt(cameraConfig.getCameraZoomRangeMax()));
        deviceInfo.setDetectMinDist(cameraConfig.getDetectMinDist());
        deviceInfo.setDetectMaxDist(cameraConfig.getDetectMaxDist());
        deviceInfo.setReverse(cameraConfig.getReverse()?-1:1);
        deviceInfo.setOriginalPushStream(cameraConfig.getPushStream());
        deviceInfo.setAiPushStream(cameraConfig.getAiPushStream());
        deviceInfo.setSmart(cameraConfig.getSmart()?1:0);
        deviceInfo.setManufacturer(cameraConfig.getManufacturer());
        deviceInfo.setModel(cameraConfig.getModel());
        deviceInfo.setFirmwareVersion(cameraConfig.getFirmwareVersion());
        deviceInfo.setSerialNumber(cameraConfig.getSerialNumber());
        return deviceInfo;
    }

}
